<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['merchid']){
	$merchid=plugin('jy_merch')->checklogin($_RQ['merchid']);
}elseif($_SESSION['merchid']){
	$merchid=plugin('jy_merch')->checklogin();
}else{
	if(!$_SESSION['uid']){
		exi('','','jy_paimerch/myhome/');
	}
	$merchid=plugin('jy_merch')->checklogin();
	$_SESSION['merchid']=$_SESSION['merchid']?:$merchid;
}
if($_RQ['op']=='main'){
	$_SESSION['logout_forward']='jy_paimerch/myhome/';
	$return['item']=pdo_get('jy_merch_user',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$return['item']['orderfee']=pdo_getsum('jy_weishop_order',array('ispay'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'price');
	$return['item']['inpayfee']=pdo_getsum('jy_merch_paylog',array('status'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'money');
	$return['item']['orderfee'] +=$return['item']['inpayfee'];
	$return['item']['ordernum']=pdo_count('jy_weishop_order',array('ispay'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'id');
	$return['item']['goodsnum']=pdo_count('jy_weishop_goods',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'id');
	$return['item']['logo']=tomedia($return['item']['logo']);
	$return['isopenvoice']=$return['item']['isopenvoice'];
	$return['merchs']=pdo_getall('jy_merch_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	$return['plugin_icon']=$return['item']['logo'];
	$this->session('plugin_icon',$return['item']['logo']);
	$avatar=pdo_getcolumn('core_members',array('id'=>$return['item']['mid']),'avatar');
	$return['item']['logo']=$return['item']['logo']?$return['item']['logo']:$avatar;
	$return['item']['url']=custom_url('jy_paimerch/merch//id='.$merchid,true);
	$return['item']['rechargeurl']=custom_url('jy_merch/member.recharge//merchid='.$merchid,true);
	$return['item']['payurl']=custom_url('jy_merch/inpay//merchid='.$merchid,true);
	if($this->plugin_exists('jy_commission')){
		$jy_commission=1;
	}
	$return['data2']['orderfee']=$return['item']['orderfee'];
	//计算待结算金额
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>3,'merchid'=>$merchid,'clearstatus'=>0);
	if($return['pluginset']['balance_day']){
		//结算天数
		$time=TIMESTAMP-intval($return['pluginset']['balance_day'])*86400;
		$params['finishtime <']=$time;
	}
	$orders=pdo_getall('jy_weishop_order',$params);
	$clearfee0=0;
	$sys_choufee=0;
	$commission_money=0;
	$dispatchprice=0;
	foreach($orders as $order){
				//完成的订单才结算
				$get_choufee=model('jy_merch/commission')->get_choufee($order,true,$merchid,1);
				$sys_choufee +=$get_choufee['commission_money'];
				if($jy_commission){
					$commission_money=model('jy_commission/commission')->get_orderfee($order['id']);
				}
				$commission_money +=$commission_money;
				$clearfee0 +=$order['price'];
				if($order['peisong_id']){
					$sys_peiprice=pdo_getcolumn('jy_merch_order',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$order['id']),'sys_peiprice');
					$sys_peiprice=$sys_peiprice>0?$sys_peiprice:$order['dispatchprice'];
					$dispatchprice +=$sys_peiprice;
				}
	}
	$clearfeeinpay=pdo_getsum('jy_merch_paylog',array('status'=>1,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clearstatus'=>0),'money');
	$clearfee0 +=$clearfeeinpay;
	$return['data2']['clearfee0']=$clearfee0-$sys_choufee-$dispatchprice;
	$return['data2']['clearfee0']=price($return['data2']['clearfee0']);
	$return['clearfee0']=price($clearfee0);
	$return['commission_money0']=price($sys_choufee);
	$return['commission_money']=price($commission_money);
	$return['dispatchprice']=price($dispatchprice);
	$return['params']=array('daytype'=>0,'time'=>'','qushi'=>0,'qushitype'=>0);
	$return['tmplIds']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>0);
	$params['status']=1;
	$return['status1']=pdo_count('jy_weishop_order',$params);
	$params['status']=0;
	$return['ispay']=pdo_count('jy_weishop_order',$params);
	$params['status']=2;
	$return['status2']=pdo_count('jy_weishop_order',$params);
	$return['goods']=pdo_count('jy_weishop_goods',array('status <>'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
	$return['delgoods']=pdo_count('jy_weishop_goods',array('status'=>3,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'id');
	$return['verifygoodsnum']=pdo_count('jy_weishop_verifygoods_log',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'salerid'=>$_SESSION['uid']),'id');
	$return['verifynum']=pdo_count('jy_weishop_order_verify',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clerk_id'=>$_SESSION['uid']),'id');
	$return['selfverifynum']=pdo_count('jy_weishop_order_self',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'clerk_id'=>$_SESSION['uid']),'id');
	$return['jy_areashop'] = $this->plugin_exists('jy_areashop');
	$return['member']=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','credit3','credit4','credit5','credit6','mobile','avatar','nickname'));
	$return['ideal_money']=pdo_getcolumn('core_ideal_money',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'money');
	$credit_types=credit_types();
	$credit_types2=array();
	foreach($credit_types as $key=>$value){
		$fee=$return['member']['credit'.$key];
		$credit_types2[$key]=array(
			'id'=>$key,
			'title'=>$value,
			'fee'=>$fee
		);
	}
	$return['credit_types']=$credit_types2;
	//判断商户独立提现
	$setting=$this->plugin_setting('setting');
	foreach($setting['cash_set'] as $cv){
		if($cv['rate'] && $cv['type']){
			$return['merchtixian']=1;
		}
	}
	//会员升级
	$return['mygroup']=model('jy_merch/model')->get_groupset($merchid);
	$return['tips']=model('jy_merch/model')->checkviptime($return['item']);	
	if($return['item']['viptime'] && $return['item']['viptime']>TIMESTAMP){
		$return['item']['viptime2']=date('Y-m-d',$return['item']['viptime']);
	}elseif($return['item']['viptime'] && $return['item']['viptime']<TIMESTAMP){
		$return['item']['viptime2']='已过期';
	}
	$storeurl='jy_paimerch/merch//id='.$merchid;
	if($_SESSION['wxapp_id']){
		$image='oss/'.$_SESSION['uniacid'].'/images/jy_paimerch/merch/wxappid'.$merchid.'.jpg';
		$isadd=cfc('files')->source_check($image);
		if(!$isadd){
			$url=wxapp_links($storeurl);
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
			if($_SESSION['fromh5wxapp']){
				$url=$storeurl;
				$url='/pages/index/index?url='.urlencode($url);
			}
			$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
			$path='oss/'.$_SESSION['uniacid'].'/images/jy_paimerch/merch';
			if(!file_exists(ROOT_AT.$path)){
				mkdir(ROOT_AT.$path,0700, true);
			}
			if($res){
				cfc('files')->source_save($res,'nochoose','png','二维码',$image,'',$url);
			}
		}
		$return['item']['qrcode']=$image;
	}
	set_html('title','商户后台');
	exi($return);
}
if($_RQ['op']=='getdata'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	//创建时间
	if($_RQ['tstart'] && $_RQ['tend']){
		$params['createtime between']=array($_RQ['tstart'],$_RQ['tend']);
	}else{
		$daytype=intval($_RQ['daytype']);
		if($daytype==0){
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$params['createtime >']=$daytime;
		}elseif($daytype==1){
			$yesdaytime = mktime(0,0,0,date('m'),date('d')-1,date('Y'));
			$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
			$params['createtime between']=array($yesdaytime,$daytime);
		}elseif($daytype==2){
			$day7time = mktime(0,0,0,date('m'),date('d')-7,date('Y'));
			$params['createtime >']=$day7time;
		}elseif($daytype==3){
			
		}
	}
	//print_r($params);exit;
	$params['ispay']=1;
	$data['paybi']=pdo_count('jy_weishop_order',$params);
	$data['payren']=pdo_count('jy_weishop_order',$params);
	$data['payyuan']=pdo_getsum('jy_weishop_order',$params,'price');
	$data['payjian']=pdo_getsum('jy_weishop_order_goods',$params,'total');
	$data['shoubi']=pdo_count('jy_weishop_order',$params);
	$data['shouren']=pdo_count('jy_weishop_order',$params);
	$data['shouyuan']=pdo_getsum('jy_weishop_order',$params,'price');
	$data['shoujian']=pdo_getsum('jy_weishop_order_goods',$params,'total');
	$return['params']=$params;
	if($_SESSION['os']=='windows'){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$access['status_day']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime));
		$access['status_all']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid']));
		$access['status1_day']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3),'createtime >'=>$daytime));
		$access['status1_all']=pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>array(1,2,3)));
		//月份
		for($i=0;$i<12;$i++){
					if($i==0){
						$yuestart = mktime(0,0,0,date('m'),1,date('Y'));
						$yueend = time();
						
					}else{
						$yuestart = mktime(0,0,0,date('m')-$i,1,date('Y'));
						$yueend = mktime(0,0,0,date('m')-$i+1,1,date('Y'));
					}
					$params=array('uniacid'=>$_SESSION['uniacid']);
					$params['createtime between']=array($yuestart,$yueend);
					$month12[$i] = date('Y-m',strtotime('-'.$i.' month'));
					//$monthaccess[]=pdo_count('core_access_log',$params);
					$accessline[$i]=pdo_count('jy_weishop_order',$params);
					//$monthcredit2[]=pdo_getsum('core_members',$params,'credit2');
		}
		$accessline=array_reverse($accessline);
		$month12=array_reverse($month12);
		$htmls=array(
					array('type'=>'data','tpltype'=>4,'one_num'=>4,'title'=>'今日/总订单数据指标','datas'=>array(
						array('title'=>'今日/总订单数','num'=>2,'data'=>array($access['status_day'],$access['status_all']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'今日/总成交数','bgcolor'=>'#009933','num'=>2,'data'=>array($access['status1_day'],$access['status1_all']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'待发货/收货数','bgcolor'=>'#3366FF','num'=>2,'data'=>array($return['status1'],$return['status2']),'datatitle'=>array('昨日','比前日')),
						array('title'=>'今日/总订单数','bgcolor'=>'#FF3300','num'=>2,'data'=>array($access['pc_day'],$access['status_all']),'datatitle'=>array('客户数','申请数')),
					)),
					array('type'=>'echarts_arealine','ecid'=>'mobile','title'=>'月度订单数据','datas'=>array(
						'datas'=>array(
							array('name'=>'订单数','value'=>$accessline),
						),
						'xdatas'=>$month12,
					)),
		);	
		//$return['htmls']=$htmls;
	}
	$return['data']=$data;
	exi($return);
}
//获取小程序订阅消息ID
if($_RQ['op']=='get_tmpids'){
	$tmplIds=array();
	$tmpid=cfc('wxapp_api')->get_tpl_template_id('1538',[7,1,2,3],'pay2seller','订单支付成功通知卖方','jy_weishop');
	if($tmpid){
		$tmplIds[]=$tmpid;
	}
	$res['list']=$tmplIds;
	exi($res);
}